package com.yunjing.tabdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivityTag";

    private TabLayout mTabLayout;
    private ViewPager viewPager;
    private ViewpagerAdapter mViewpagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabLayout = findViewById(R.id.tab_layout);
        viewPager = findViewById(R.id.viewPager);

        List<TabEntity> tabEntities = TabEntity.getTabEntities(true);
        mViewpagerAdapter = new ViewpagerAdapter(getSupportFragmentManager());
        mViewpagerAdapter.setPageData(tabEntities);

        viewPager.setAdapter(mViewpagerAdapter);
        mTabLayout.setupWithViewPager(viewPager);
        //指定加载的页数
        viewPager.setOffscreenPageLimit(tabEntities.size());
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.d(TAG, "onTabSelected-tab: " + tab.getText());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "onTabSelected-vp: ");
                if ("待发料".equals(tabEntities.get(position).getTabName())) {
                    updateTab(true);
                } else if ("待倒运".equals(tabEntities.get(position).getTabName())) {
                    updateTab(false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void updateTab(boolean isAlong) {
        List<TabEntity> tabEntities = TabEntity.getTabEntities(isAlong);
        mViewpagerAdapter.setPageData(tabEntities);
        //指定加载的页数
        viewPager.setOffscreenPageLimit(tabEntities.size());
        mViewpagerAdapter.notifyDataSetChanged();
    }

    public class ViewpagerAdapter extends FragmentPagerAdapter {


        private List<TabEntity> tabEntities;

        public void setPageData(List<TabEntity> tabEntities) {
            this.tabEntities = tabEntities;
        }

        public ViewpagerAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        //获取tab导航文本
        @Override
        public CharSequence getPageTitle(int position) {
            Log.d(TAG, "getPageTitle: ");
            return tabEntities.get(position).getTabName();
        }

        @Override
        public int getItemPosition(Object object) {
            Log.d(TAG, "getItemPosition: ");
            return POSITION_NONE;
        }


        @NonNull
        @Override
        public Fragment getItem(int position) {
            Log.d(TAG, "getItem---position: " + position);
            return TabFragment.newInstance(tabEntities.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TabFragment tabFragment = (TabFragment) super.instantiateItem(container, position);
            TabFragment.setTabEntity(tabFragment, tabEntities.get(position));
            return tabFragment;
        }


        @Override
        public int getCount() {
            Log.d(TAG, "getCount: ");
            return tabEntities.size();
        }

    }

}